package disruptor.my;

/**
 * 循环队列
 * 为了区分队列的 “满”和“空”状态， 因此队列需要预留一个位置，还剩一个位置的时候就认为这个队列满了
 * 这样，只有空的状态 head和tail是重叠的
 */
public class Queue {
    private Long[] data;
    private int size = 0, head = 0, tail = 0;

    public Queue(int size) {
        this.data = new Long[size];
        this.size = size;
    }

    public static void main(String[] args) {
        Queue queue = new Queue(5);
        System.out.println(queue.add(1L));
        System.out.println(queue.add(1L));
        System.out.println(queue.add(1L));
        System.out.println(queue.add(1L));
        System.out.println(queue.add(1L));
        System.out.println(queue.add(1L));

    }

    public boolean add(Long element) {
        if ((tail + 1) % size == head) {
            return false;
        }
        data[tail] = element;
        tail = (tail + 1) % size;
        return true;
    }

    public Long poll() {
        if (head == tail) {
            return null;
        }
        long ret = data[head];
        head = (head + 1) % size;
        return ret;
    }
}


class Producer {
    private Queue queue;

    public Producer(Queue queue) {
        this.queue = queue;
    }

    public void produce(Long data) throws InterruptedException {
        while (!queue.add(data)) {
            Thread.sleep(100);
        }
    }
}

class Consumer {
    private Queue queue;

    public Consumer(Queue queue) {
        this.queue = queue;
    }

    public void consume() throws InterruptedException {
        while (true) {
            Long data = queue.poll();
            if (data == null) {
                Thread.sleep(100);
            } else {
                // TODO: ... 消费数据的业务逻辑
            }
        }
    }
}

